﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using SanctusSoftware.Music.ChordSheetEditor.Client;
using System.Windows.Forms;
//using SanctusSoftware.Logging;
//using SanctusSoftware.Licensing;
using System.Reflection;
using SanctusSoftware.Music.ChordSheetEditor.Wpf;
using System.Windows.Forms.Integration;
//using SanctusSoftware.Licensing.Releases;

namespace Chord_Sheet_Add_in_for_Word_2007
{
  public partial class ChordSheetRibbon : OfficeRibbon, IChordSheetEditorActionView
  {

    #region Constructor

    public ChordSheetRibbon()
    {
      InitializeComponent();
      SetupTargetGallery();
    }

    #endregion

    #region IChordSheetEditorActionView Members

    #region Misc

    private bool _IsDevMode = false;
    public bool IsDevMode
    {
      get { return _IsDevMode; }
      set 
      { 
        _IsDevMode = value;
        _ChordSheetTab.Label = "Chord Sheet";
        if (_IsDevMode)
        {
          _ChordSheetTab.Label += " (Dev)";
        }
      }
    }
    

    #endregion

    #region Target

    public event EventHandler TargetChanged;

    protected void OnTargetChanged()
    {
      if (TargetChanged != null)
      {
        TargetChanged(this, new EventArgs());
      }
    }

    public ActionTarget Target
    {
      get
      {
        if (targetGallery.Tag != null)
        {
          return (ActionTarget)targetGallery.Tag;
        }
        else
        {
          throw new Exception("No target defined");
        }
      }
      set
      {
        switch (value)
        {
          case ActionTarget.NewDocument:
            targetGallery.SelectedItem = targetGallery.Items[0];
            SetupSelectedItem();
            break;
          case ActionTarget.ThisDocument:
            targetGallery.SelectedItem = targetGallery.Items[1];
            SetupSelectedItem();
            break;
          case ActionTarget.CurrentSelection:
            targetGallery.SelectedItem = targetGallery.Items[2];
            SetupSelectedItem();
            break;
          case ActionTarget.CurrentPage:
            targetGallery.SelectedItem = targetGallery.Items[3];
            SetupSelectedItem();
            break;
          default:
            break;
        }
      }
    }

    #endregion

    #region Transpose

    public string[] ValidKeys
    {
      set
      {

        // TODO: Maybe save this somewhere

        foreach (string key in value)
        {
          RibbonDropDownItem item = new RibbonDropDownItem();
          item.Label = key;
          item.Tag = key;
          Globals.Ribbons.ChordSheetRibbon.originalKeyDropDown.Items.Add(item);
        }

        foreach (string key in value)
        {
          RibbonDropDownItem item = new RibbonDropDownItem();
          item.Label = key;
          item.Tag = key;
          Globals.Ribbons.ChordSheetRibbon.transposeToDropDown.Items.Add(item);
        }
      }
    }

    public event EventHandler RequestTranspose;

    protected void OnRequestTranspose()
    {
      if (RequestTranspose != null)
      {
        RequestTranspose(this, new EventArgs());
      }
    }

    public event EventHandler RequestTransposeOptions;

    protected void OnRequestTransposeOptions()
    {
      if (RequestTransposeOptions != null)
      {
        RequestTransposeOptions(this, new EventArgs());
      }
    }


    public event EventHandler OriginalKeyChanged;

    protected void OnOriginalKeyChanged()
    {
      if (OriginalKeyChanged != null)
      {
        OriginalKeyChanged(this, new EventArgs());
      }
    }

    public string OriginalKey
    {
      get
      {
        if (this.originalKeyDropDown.SelectedItem == null)
        {
          return null;
        }
        else
        {
          return (string)this.originalKeyDropDown.SelectedItem.Tag;
        }
      }
      set
      {
        foreach (RibbonDropDownItem item in this.originalKeyDropDown.Items)
        {
          if (item.Tag.ToString() == value)
          {
            this.originalKeyDropDown.SelectedItem = item;
            break;
          }
        }
      }
    }

    public event EventHandler TransposeToChanged;

    protected void OnTransposeToChanged()
    {
      if (TransposeToChanged != null)
      {
        TransposeToChanged(this, new EventArgs());
      }
    }

    public string TransposeTo
    {
      get
      {
        if (this.transposeToDropDown.SelectedItem == null)
        {
          return null;
        }
        else
        {
          return (string)this.transposeToDropDown.SelectedItem.Tag;
        }
      }
      set
      {
        foreach (RibbonDropDownItem item in this.transposeToDropDown.Items)
        {
          if (item.Tag.ToString() == value)
          {
            this.transposeToDropDown.SelectedItem = item;
            break;
          }
        }
      }
    }

    #endregion

    #region Quick Transpose

    public bool IsQuickTransposeDownLowEnabled
    {
      get
      {
        return quickTransposeDownOneLowButton.Enabled;
      }
      set
      {
        quickTransposeDownOneLowButton.Enabled = value;
      }
    }

    public string QuickTransposeDownLowKey
    {
      get
      {
        if (quickTransposeDownOneLowButton.Tag == null)
        {
          return null;
        }
        else
        {
          return (string)quickTransposeDownOneLowButton.Tag;
        }
      }
      set
      {
        quickTransposeDownOneLowButton.Tag = value;
        quickTransposeDownOneLowButton.Label = value;
      }
    }

    public event EventHandler RequestQuickTransposeDownLow;

    protected void OnRequestQuickTransposeDownLow()
    {
      if (RequestQuickTransposeDownLow != null)
      {
        RequestQuickTransposeDownLow(this, new EventArgs());
      }
    }

    public bool IsQuickTransposeDownHighEnabled
    {
      get
      {
        return quickTransposeDownOneHighButton.Enabled;
      }
      set
      {
        quickTransposeDownOneHighButton.Enabled = value;
      }
    }

    public string QuickTransposeDownHighKey
    {
      get
      {
        if (quickTransposeDownOneHighButton.Tag == null)
        {
          return null;
        }
        else
        {
          return (string)quickTransposeDownOneHighButton.Tag;
        }
      }
      set
      {
        quickTransposeDownOneHighButton.Tag = value;
        quickTransposeDownOneHighButton.Label = value;
      }
    }

    public event EventHandler RequestQuickTransposeDownHigh;

    protected void OnRequestQuickTransposeDownHigh()
    {
      if (RequestQuickTransposeDownHigh != null)
      {
        RequestQuickTransposeDownHigh(this, new EventArgs());
      }
    }

    public bool IsQuickTransposeEnharmonicEnabled
    {
      get
      {
        return quickTransposeEnharmonicButton.Enabled;
      }
      set
      {
        quickTransposeEnharmonicButton.Enabled = value;
      }
    }

    public string QuickTransposeEnharmonicKey
    {
      get
      {
        if (quickTransposeEnharmonicButton.Tag == null)
        {
          return null;
        }
        else
        {
          return (string)quickTransposeEnharmonicButton.Tag;
        }
      }
      set
      {
        quickTransposeEnharmonicButton.Tag = value;
        quickTransposeEnharmonicButton.Label = value;
      }
    }

    public event EventHandler RequestQuickTransposeEnharmonic;

    protected void OnRequestQuickTransposeEnharmonic()
    {
      if (RequestQuickTransposeEnharmonic != null)
      {
        RequestQuickTransposeEnharmonic(this, new EventArgs());
      }
    }

    public bool IsQuickTransposeUpLowEnabled
    {
      get
      {
        return quickTransposeUpOneLowButton.Enabled;
      }
      set
      {
        quickTransposeUpOneLowButton.Enabled = value;
      }
    }

    public string QuickTransposeUpLowKey
    {
      get
      {
        if (quickTransposeUpOneLowButton.Tag == null)
        {
          return null;
        }
        else
        {
          return (string)quickTransposeUpOneLowButton.Tag;
        }
      }
      set
      {
        quickTransposeUpOneLowButton.Tag = value;
        quickTransposeUpOneLowButton.Label = value;
      }
    }

    public event EventHandler RequestQuickTransposeUpLow;

    protected void OnRequestQuickTransposeUpLow()
    {
      if (RequestQuickTransposeUpLow != null)
      {
        RequestQuickTransposeUpLow(this, new EventArgs());
      }
    }

    public bool IsQuickTransposeUpHighEnabled
    {
      get
      {
        return quickTransposeUpOneHighButton.Enabled;
      }
      set
      {
        quickTransposeUpOneHighButton.Enabled = value;
      }
    }

    public string QuickTransposeUpHighKey
    {
      get
      {
        if (quickTransposeUpOneHighButton.Tag == null)
        {
          return null;
        }
        else
        {
          return (string)quickTransposeUpOneHighButton.Tag;
        }
      }
      set
      {
        quickTransposeUpOneHighButton.Tag = value;
        quickTransposeUpOneHighButton.Label = value;
      }
    }

    public event EventHandler RequestQuickTransposeUpHigh;

    protected void OnRequestQuickTransposeUpHigh()
    {
      if (RequestQuickTransposeUpHigh != null)
      {
        RequestQuickTransposeUpHigh(this, new EventArgs());
      }
    }

    #endregion

    #region Chords

    public event EventHandler RequestFormatChords;

    protected void OnRequestFormatChords()
    {
      if (RequestFormatChords != null)
      {
        RequestFormatChords(this, new EventArgs());
      }
    }

    public event EventHandler RequestRemoveHyperlinksFromChordLines;

    protected void OnRequestRemoveHyperlinksFromChordLines()
    {
      if (RequestRemoveHyperlinksFromChordLines != null)
      {
        RequestRemoveHyperlinksFromChordLines(this, new EventArgs());
      }
    }

    #endregion

    #region Lyrics

    public event EventHandler RequestRemoveChords;

    protected void OnRequestRemoveChords()
    {
      if (RequestRemoveChords != null)
      {
        RequestRemoveChords(this, new EventArgs());
      }
    }

    #endregion

    #endregion

    #region Setup

    private void SetupTargetGallery()
    {
      targetGallery.Click += new EventHandler<RibbonControlEventArgs>(targetGallery_Click);
      targetGallery.Items[0].Tag = ActionTarget.NewDocument;
      targetGallery.Items[1].Tag = ActionTarget.ThisDocument;
      targetGallery.Items[2].Tag = ActionTarget.CurrentSelection;
      targetGallery.Items[3].Tag = ActionTarget.CurrentPage;
      targetGallery.SelectedItem = targetGallery.Items[1];
      SetupSelectedItem();
    }

    private void SetupSelectedItem()
    {
      targetGallery.Label = targetGallery.SelectedItem.Label;
      targetGallery.Image = targetGallery.SelectedItem.Image;
      targetGallery.Tag = targetGallery.SelectedItem.Tag;
    }

    #endregion

    #region Event Handlers

    private void ChordSheetRibbon_Load(object sender, RibbonUIEventArgs e)
    {

    }

    #region Target

    void targetGallery_Click(object sender, RibbonControlEventArgs e)
    {
      SetupSelectedItem();
      OnTargetChanged();
    }


    #endregion

    #region Lyrics

    private void makeLyricSheetButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestRemoveChords();
    }

    #endregion

    #region Transpose

    private void originalKeyDropDown_SelectionChanged(object sender, RibbonControlEventArgs e)
    {
      OnOriginalKeyChanged();
    }

    private void transposeToDropDown_SelectionChanged(object sender, RibbonControlEventArgs e)
    {
      OnTransposeToChanged();
    }

    private void transposeOptionsButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestTransposeOptions();
    }

    private void transposeButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestTranspose();
    }

    #endregion

    #region Chords

    private void formatChordsButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestFormatChords();
    }

    private void formatChordsGalleryButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestFormatChords();
    }

    private void removeChordHyperlinksGalleryButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestRemoveHyperlinksFromChordLines();
    }

    #endregion

    #region Quick Transpose

    private void quickTransposeDownOneLowButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestQuickTransposeDownLow();
    }

    private void quickTransposeDownOneHighButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestQuickTransposeDownHigh();
    }

    private void quickTransposeEnharmonicButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestQuickTransposeEnharmonic();
    }

    private void quickTransposeUpOneLowButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestQuickTransposeUpLow();
    }

    private void quickTransposeUpOneHighButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestQuickTransposeUpHigh();
    }

    #endregion

    #region Formatting


    #endregion

    #endregion

    #region About

    private void aboutButton_Click(object sender, RibbonControlEventArgs e)
    {
      try
      {
        //new AboutForm().ShowDialog();
        AboutWindow window = new AboutWindow();
        ElementHost.EnableModelessKeyboardInterop(window);
        window.ShowDialog();
      }
      catch (Exception ex)
      {
        MessageBox.Show("Error: " + ex.Message);
        //ExceptionLogger.Log(ex);
      }

    }

    #endregion

  }
}